/**
 * Created by gongxun on 16/12/1.
 */

var GameLoaderScene = cc.Scene.extend({
    label : null,
    ctor : function(){
        this._super();
        var self = this;

        // bg
        var bgLayer = new cc.LayerColor(cc.color(15, 15, 15, 255));
        this.addChild(bgLayer);

        //image
        var logoWidth = 160;
        var logoHeight = 200;
        if(mo._lodingImageBg){
            //loading logo
            cc.loader.loadImg(mo._lodingImageBg, {isCrossOrigin : false }, function(err, img){
                if(err){
                    var logo = new cc.Sprite("res/ccs/login/hallbg.jpg");
                    logoWidth = logo.getContentSize().width;
                    logoHeight = logo.getContentSize().height;
                    logo.setScale(mo.curSize.x / logoWidth, mo.curSize.y / logoHeight);
                    logo.setPosition(cc.p(mo.curSize.x / 2, mo.curSize.y / 2));
                    self.addChild(logo);
                }else{
                    logoWidth = img.width;
                    logoHeight = img.height;

                    var texture2d = new cc.Texture2D();
                    texture2d.initWithElement(img);
                    texture2d.handleLoadedTexture();

                    var logo = new cc.Sprite(texture2d);
                    logo.setScale(mo.curSize.x / logoWidth, mo.curSize.y / logoHeight);
                    logo.setPosition(cc.p(mo.curSize.x / 2, mo.curSize.y / 2));
                    self.addChild(logo);
                }
            });
        }

        var fontSize = 30;
        this.label = new cc.LabelTTF("Loading... 0%", "Arial", fontSize);
        this.label.setPosition(cc.p(mo.curSize.x / 2, 100));
        this.label.setColor(cc.color(255, 255, 255));
        this.addChild(this.label, 1);
    },

    onEnter: function () {
        this._super();
        this.schedule(this._startLoading, 0.3);
    },

    onExit: function () {
        this._super();
    },

    initWithResources: function (resources, cb, target) {
        if(cc.isString(resources))
            resources = [resources];
        this.resources = resources || [];
        this.cb = cb;
        this.target = target;
    },

    _startLoading: function () {
        var self = this;
        this.unschedule(this._startLoading);
        var res = this.resources;
        cc.loader.load(res,
            function (result, count, loadedCount) {
                var percent = (loadedCount / count * 100) | 0;
                percent = Math.min(percent, 100);
                self.label.setString("Loading... " + percent + "%");
            }, function () {
                if (self.cb)
                    self.cb.call(self.target);
            });
    }
});

mo.gameLoaderScene = null;

mo.LoaderScene = function(resources, cb, target){
    if(!mo.gameLoaderScene) {
        mo.gameLoaderScene = new GameLoaderScene();
    }
    mo.gameLoaderScene.initWithResources(resources, cb, target);

    cc.director.runScene(mo.gameLoaderScene);
    return mo.gameLoaderScene;
};